﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

public class cmpUser
{
    public string Login;
    public string password; 
    public string First_name;
    public string Last_name;
    public string email;
    public string phone;

    private string activation_code; // For activation link generation

    public string log_module = "authProcessor"; 

    public cmpUser()
    {
        activation_code = "";   
    }

    public int get_id()
    {
        dbProcessor my_db = new dbProcessor();

        return my_db.get_user_id(this.Login);
    }

    public int get_id(string Login)
    {
        dbProcessor my_db = new dbProcessor();

        return my_db.get_user_id(Login);  
    }

    public string get_full_name(int userId)
    {
        dbProcessor my_db = new dbProcessor();

        return my_db.get_full_name(userId); 
    }
    public string[] get_exist_logins()
    {
        dbProcessor my_db = new dbProcessor();

        return   my_db.get_all_logins();      
    }
   
    public void create_user(out MembershipCreateStatus results_status)
    {
        dbProcessor my_db = new dbProcessor();

        activation_code=new_activation_code();

        my_db.post_user(Login, First_name, Last_name, email, phone, activation_code); // Post to MAIN database

        string question = "a";
        string answer = "b";

        MembershipCreateStatus Status=new MembershipCreateStatus(); 
        
        MembershipUser new_mem_user=Membership.CreateUser(this.Login, this.password, this.email,question, answer,false,out Status); // Post to SECURITY database
        
              
        send_activation_link(activation_code);

        activation_code = "";
        
        results_status = Status; 
    }

    public bool activate_user(string act_code) 
    {
        dbProcessor my_db = new dbProcessor();
        string user_name = my_db.get_user_login(act_code); 
                        
        MembershipUser user = Membership.GetUser(user_name);

        if (!user.IsApproved)
        {
            user.IsApproved = true;
        }

        Membership.UpdateUser(user);
       
        return true; 
    }

    public void send_activation_link(string act_code)
    {
        mailProcessor ms = new mailProcessor();
        ms.SendActivationLink(email, create_activation_link(act_code)); 
    }

    private string new_activation_code()
    {
        System.Guid gd = new Guid();

        gd = System.Guid.NewGuid();

        return gd.ToString(); 
    }

    private string create_activation_link(string code)
    {
        string link = "http://localhost:17027/Deimos/Activate.aspx?act_code=" + code; 
                
        return link;
    }

}

public class authProcessor
{
    
    public authProcessor()
	{
	    
    }
  
}


